//
// Mouse Gesture Recognizer
// (c) Robert Hahn / HTW Dresden
//
// ActivationFunction.cs
// ---------------------
// Interface and implementation of activation functions.
//

using System;


namespace CNeural
{	
	/// <summary>
	/// Provides an interface for activation functions.
	/// </summary>
	public interface IActivationFunction
	{
		string Description { get; }		
		double Compute(double input);
		double ComputeDerivation(double input);
	}

	
	/// <summary>
	/// Provides a sigmoid activation function.
	/// </summary>
	[Serializable]
	public class SigmoidFunction : IActivationFunction
	{
		#region Fields
		private double gain;
		#endregion
		
		#region Properties
		/// <summary>
		/// Gets the description of the function.
		/// </summary>
		public string Description 
		{
			get { return "Sigmoid"; }
		}
	
		/// <summary>
		/// Gets or sets the gain factor of the sigmoid function.
		/// </summary>
		public double Gain 
		{
			get { return this.gain; }
			set { this.gain = value; }
		}
		#endregion
		
		#region Methods
		public double Compute(double input) 
		{
			return (double) (1.0 / (1.0 + Math.Exp(-this.gain * input)));
		}
		
		public double ComputeDerivation(double input) 
		{
			double y = Compute(input);
			return (y * (1 - y));
		}
		#endregion

		#region Constructors
		public SigmoidFunction()
		{			
			this.gain = 1F;
		}
		#endregion
	}
}
